# -*- coding: utf-8 -*-
# @Time    : 2025/10/13
# @Author  : Qwen
# @File    : MapCostController.py
# @Describe: 地图费用计算接口控制器

from flask import Blueprint, request, jsonify, current_app
from luck.map.cost.service.MapCostService import MapCostService
from luck.core.LuckConstant import LuckConstant
from luck.util.LuckLogHelperUtils import LuckLogHelperUtils

# 创建蓝图
cost_bp = Blueprint('cost_bp', __name__, url_prefix='/map/cost/mapCost')

@cost_bp.route('/noGetwayReadCost', methods=['POST'])
def calculate_cost():
    """
    计算费用接口
    请求参数:
    - distance: 里程数 (公里)
    - unitPrice: 单价 (元/公里)
    """
    try:
        # 获取请求参数
        data = request.get_json()
        if not data:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "请求参数不能为空"
            }), 400
        print(data)
        distance = data.get('distance')
        unitPrice = data.get('unitPrice')
        
        # 参数验证
        if distance is None or unitPrice is None:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "里程数和单价不能为空"
            }), 400
            
        # 验证参数类型
        try:
            distance = float(distance)
            unitPrice = float(unitPrice)
        except ValueError:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "里程数和单价必须为数字"
            }), 400
            
        # 验证参数范围
        if distance < 0 or unitPrice < 0:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "里程数和单价不能为负数"
            }), 400
            
        # 记录请求日志
        config_data = current_app.config.get('CONFIG_DATA', {})
        log = LuckLogHelperUtils(config_data)
        log.add_visit_log({
            "requri": request.path,
            "reqtype": request.mimetype,
            "reqmethod": request.method,
            "reqparam": str(data),
            "reqnote": "费用计算接口请求"
        })
        
        # 创建费用计算服务实例
        cost_service = MapCostService()
        
        # 调用服务计算费用
        result = cost_service.calculate_cost(distance, unitPrice)
        
        if result["code"] == LuckConstant.CODE_SUCCESS:
            # 记录成功响应日志
            log.add_visit_log({
                "requri": request.path,
                "reqtype": request.mimetype,
                "reqmethod": request.method,
                "reqparam": str(data),
                "reqnote": "费用计算接口响应成功"
            })
            return jsonify({
                "code": LuckConstant.CODE_SUCCESS,
                "msg": LuckConstant.MSG_SUCCESS,
                "data": result["data"]
            }), 200
        else:
            # 记录失败响应日志
            log.add_error_log({
                "requri": request.path,
                "reqtype": request.mimetype,
                "reqmethod": request.method,
                "reqparam": str(data),
                "reqnote": "费用计算接口响应失败",
                "reqerror": result["msg"]
            })
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": result["msg"]
            }), 500
            
    except Exception as e:
        # 记录错误日志
        config_data = current_app.config.get('CONFIG_DATA', {})
        log = LuckLogHelperUtils(config_data)
        log.add_error_log({
            "requri": request.path,
            "reqtype": request.mimetype,
            "reqmethod": request.method,
            "reqparam": str(request.get_json() if request.is_json else {}),
            "reqnote": "费用计算接口异常",
            "reqerror": str(e)
        })
        
        return jsonify({
            "code": LuckConstant.CODE_FAIL,
            "msg": f"服务内部错误: {str(e)}"
        }), 500